<?php

namespace WebmanGrpc\Examples;

use WebmanGrpc\Client\GrpcClient;

/**
 * 示例gRPC客户端
 * 
 * 展示如何使用gRPC客户端调用服务
 */
class ExampleClient
{
    /**
     * 调用简单方法
     * 
     * @param string $address 服务地址
     * @param string $name 名称
     * @return string 响应消息
     */
    public function callSimpleMethod(string $address, string $name): string
    {
        // 创建客户端
        $client = new GrpcClient($address);
        
        // 创建请求
        $request = new class extends \Google\Protobuf\Internal\Message {
            protected $name;
            
            public function setName($name) {
                $this->name = $name;
            }
            
            public function getName() {
                return $this->name;
            }
        };
        $request->setName($name);
        
        // 调用服务
        $response = $client->call('example.ExampleService', 'SimpleMethod', $request);
        
        return $response->getMessage();
    }
    
    /**
     * 调用流式方法
     * 
     * @param string $address 服务地址
     * @param string $name 名称
     * @return array 响应消息列表
     */
    public function callStreamMethod(string $address, string $name): array
    {
        // 创建客户端
        $client = new GrpcClient($address);
        
        // 创建请求
        $request = new class extends \Google\Protobuf\Internal\Message {
            protected $name;
            
            public function setName($name) {
                $this->name = $name;
            }
            
            public function getName() {
                return $this->name;
            }
        };
        $request->setName($name);
        
        // 调用流式服务
        $responses = [];
        $stream = $client->stream('example.ExampleService', 'StreamMethod', $request);
        
        foreach ($stream as $response) {
            $responses[] = $response->getMessage();
        }
        
        return $responses;
    }
    
    /**
     * 调用双向流方法
     * 
     * @param string $address 服务地址
     * @param array $names 名称列表
     * @return array 响应消息列表
     */
    public function callBidirectionalStream(string $address, array $names): array
    {
        // 创建客户端
        $client = new GrpcClient($address);
        
        // 创建请求流
        $requestStream = (function() use ($names) {
            foreach ($names as $name) {
                $request = new class extends \Google\Protobuf\Internal\Message {
                    protected $name;
                    
                    public function setName($name) {
                        $this->name = $name;
                    }
                    
                    public function getName() {
                        return $this->name;
                    }
                };
                $request->setName($name);
                yield $request;
            }
        })();
        
        // 调用双向流服务
        $responses = [];
        $stream = $client->stream('example.ExampleService', 'BidirectionalStream', $requestStream);
        
        foreach ($stream as $response) {
            $responses[] = $response->getMessage();
        }
        
        return $responses;
    }
}